<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Model\Cate;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    /**  加载文章列表
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = self::cate();
        return view('/admin.cate.list',['data'=>$data]);
    }

    /**   访问添加分类
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //用格式化处理分类数据 静态方法
       
        $data = self::cate();
        return view('/admin.cate.add',['data'=>$data]);
    }

    /**   添加类别
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
         //验证提交数据是否为空
        $this->validate($request, [
            'name' => 'required',
            
         ],[
            'name.required'=>'分类名称不能为空',
         ]);

        //查询name名称
        $data = $request->only('name');
        
        //判断父类的pid是否为0 如果是零,添加为顶级类  
        if($request->input('pid')==0){
            $data['pid'] = 0;
            $data['path'] = 0;

        }else{
            //子类的pid等于父类的iD
            $data['pid'] = $request->input('pid');  
            //子类的path = 父类的path加父类的id;
            $data['path'] = DB::table('goods')->where('id','=',$request->input('pid'))->first()['path'].','.$request->input('pid');
        }
       
        if(DB::table('goods')->insert($data)){
            return redirect('/cate')->with('success','添加分类成功');
        }else{
            return back()->with('error','添加失败');
        }
      
    }

    /**  在列表里添加文章
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {   

        $data = self::cate();
        return view('/admin.cate.list_add',['data'=>$data,'id'=>$id]);
    }


     /**
    *   无限分类静态方法
    *   /cate/cate
    */

    public static function cate(){
        $data = DB::table('goods')
            ->select(DB::raw("*,concat(path,',',id) as paths"))
            ->orderBy('paths')
            ->get();
        foreach($data as $k=>$v){
            $data[$k]['name'] =str_repeat('|---',count(explode(',',$v['path']))-1).$v['name'];
        }
           
        return $data;
    }

    /**  访问修改页面
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {   
        $data = DB::table('goods')->where('id','=',$id)->first();
        // dd($data);
        return view('/admin.cate.edit',['data'=>self::cate(),'vo'=>$data]);
    }

    /**   
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   

       if(DB::table('goods')->where('id',$id)->update(['name'=>$request->input('name')])){
            return redirect('/cate')->with('success','修改成功');
       }else{
            return back()->with('error','修改失败');
       }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

   

    //获取分类类表的pid 将父类的id转化为name
    public static function tran($pid){
       //三元运算符
        return ($pid==0)?'顶级父类':DB::table('goods')->where('id','=',$pid)->first()['name'];
    }

    /*   删除分类
    *   /cate_del
    */
    //如果分类下有子类则不能删除,如果没有 可以删除   父类的id = 子类的pid 判断子类的pid有没有等于父类的id
    public function cate_del($id){
        if(DB::table('goods')->where('pid','=',$id)->count()>0){

            return back()->with('error','删除失败');
        }else{
            if(DB::table('goods')->where('id',$id)->delete()){
                return redirect('/cate')->with('success','删除成功');
            }else{
                return back()->with('error','删除失败');
            }
        }
    }
}
